package cn.edu.hqu.exp.courseselection.web;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.edu.hqu.exp.courseselection.domain.PersonInfo;
import cn.edu.hqu.exp.courseselection.domain.User;
import cn.edu.hqu.exp.courseselection.repository.PersonInfoRepository;
import cn.edu.hqu.exp.courseselection.repository.UserRepository;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/conclude")
public class ConcludeController {
	private PersonInfoRepository personInfoRepo;
	private UserRepository userRepo;
	
	@Autowired
	public ConcludeController(
			PersonInfoRepository personInfoRepo,
			UserRepository userRepo
			) {
		this.personInfoRepo = personInfoRepo;
		this.userRepo = userRepo;
	}
	
	@GetMapping
	public String showResult(Model model,Principal principal) {
		List <PersonInfo> personInfos = new ArrayList<>();
		if(principal != null) {
			String username=principal.getName();
			User user=userRepo.findByUsername(username);
			model.addAttribute("nowUser",user);
			personInfoRepo.findByUser(user).forEach(i -> personInfos.add(i));
		}
		model.addAttribute("personInfos",personInfos);
		log.info("person: "+personInfos);
		return "conclude"; 
		
	}
}
